package cn.smart.rest.controller;

import cn.smart.service.FtpService;
import cn.smart.util.ExceptionUtil;
import cn.smart.util.Info;
import cn.smart.util.SmartResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Xiezh
 * @Description:文件上传
 * @create 2017/8/15
 */
@Controller
@RequestMapping(value = "/ftp")
public class FtpController {
    @Autowired
    private FtpService ftpService;

    @ResponseBody
    @RequestMapping("/upload/{type}/{id}")
    public SmartResult uploadFile(MultipartFile file, @PathVariable(value = "type") int type,
                                  @PathVariable(value = "id") int id) {
        if(StringUtils.isEmpty(file.getOriginalFilename())){
            return new SmartResult(200,Info.SUCCESS,"没有文件上传");
        }

        try {
            SmartResult smartResult = ftpService.uploadFile(file, type, id);
            return smartResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new SmartResult(500, Info.FAIL, ExceptionUtil.getStackTrace(e));
        }
    }
}
